package com.sinog.biz.config;

import lombok.Data;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import org.springframework.stereotype.Component;
import java.io.IOException;

/**
 * @desc Yaml配置
 * @author wangxj
 * @date 2022/6/29 下午6:32
 */
@Component
@Data
public class YamlPropertySourceFactory implements PropertySourceFactory {

    /**
     * yaml 文档解析方法
     * @param name 配置项名称
     * @param resource 配置项资源
     * @return PropertySource<?>
     */
    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
        // 返回 yaml 属性资源
        return new YamlPropertySourceLoader()
                .load (resource.getResource ().getFilename (), resource.getResource ())
                .get (0);
    }
}